package com.yidin.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

/**
 * 需要权限android.permission.WRITE_EXTERNAL_STORAGE
 * 
 * @author cola
 * @since 20120718
 */
public class FileAdapter {
	// private static final String TAG = "FileAdaptor";

	/**
	 * 判断
	 */
	public static boolean isSdCardExist() {
		return Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
	}

	public static boolean isFileExist(String path) {
		File file = new File(path);
		return file.isFile();
	}

	/**
	 * 目录相关
	 */
	public static String getSdCardRootDir() {
		return Environment.getExternalStorageDirectory().getPath() + "/";
	}

	public static String getCacheDir(Context ctx) {
		// /data/data/<package name>/cache
		return ctx.getCacheDir().getPath() + "/";
	}

	public static String getFilesDir(Context ctx) {
		// /data/data/<package name>/files
		return ctx.getFilesDir().getPath() + "/";
	}

	public static boolean mkDir(String path) {
		File dir = new File(path);
		if (!dir.exists())
			return dir.mkdirs();
		else
			return false;
	}

	/**
	 * 文件操作相关
	 */
	public static boolean writeFile(String path, InputStream is) {
		boolean result = false;
		FileOutputStream os = null;
		BufferedOutputStream bos = null;
		try {
			File file = new File(path);
			os = new FileOutputStream(file, false);
			bos = new BufferedOutputStream(os);
			int readLen = 0;
			byte[] buf = new byte[1024];
			while ((readLen = is.read(buf)) != -1) {
				bos.write(buf, 0, readLen);
			}
			bos.flush();
			bos.close();
			os.close();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bos.close();
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	/**
	 * 文件操作相关
	 */
	public static boolean writeFile(String path, InputStream is, String fileName) {
		boolean result = false;
		FileOutputStream os = null;
		BufferedOutputStream bos = null;
		try {
			File file = new File(path);
			os = new FileOutputStream(file, false);
			bos = new BufferedOutputStream(os);
			int readLen = 0;
			byte[] buf = new byte[1024];
			while ((readLen = is.read(buf)) != -1) {
				bos.write(buf, 0, readLen);
			}
			bos.flush();
			bos.close();
			os.close();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bos.close();
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	public static boolean writeTextFile(String path, String data) {
		boolean result = false;
		FileWriter fw = null;
		try {
			File file = new File(path);
			fw = new FileWriter(file);
			fw.write(data);
			fw.close();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	public static InputStream readFile(String path) {
		File file = new File(path);
		FileInputStream fis = null;

		try {
			fis = new FileInputStream(file);
		} catch (Exception e) {

		}

		return fis;
	}

	public static boolean copyFile(File srcFile, String descPath) {
		try {
			FileInputStream fis = new FileInputStream(srcFile);
			File desc = new File(descPath);
			if (!desc.exists()) {
				new File(KDesPath).mkdirs();
				desc.createNewFile();
			} else {
				return true;
			}
			FileOutputStream fos = new FileOutputStream(desc);
			byte[] buffer = new byte[1024];
			int length = 0;
			while ((length = fis.read(buffer)) != -1) {
				fos.write(buffer, 0, length);
			}
			fos.flush();
			fos.close();
			fis.close();
			fos = null;
			fis = null;
			System.out.println("拷贝成功~！");
			// srcFile.delete();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 安装下载的应用
	 * 
	 * @param path
	 * @param activity
	 */
	public static void install(String path, Activity activity) {
		File file = new File(path);
		Intent it = new Intent(Intent.ACTION_VIEW);
		it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		it.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		activity.startActivity(it);
	}

	public static String KSrcPath = String.format("%s%s%s%s%s%s%s%s",
			Environment.getExternalStorageDirectory().getPath(),
			File.separator, "Marketing", File.separator, "data",
			File.separator, "temp", File.separator);

	public static String KDesPath = String.format("%s%s%s%s%s%s%s%s",
			Environment.getExternalStorageDirectory().getPath(),
			File.separator, "Marketing", File.separator, "downloadMgr",
			File.separator, "icon", File.separator);
}
